package com.yanqu.road.server.manger.activity.chefduel;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuel2DishConfig;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuel2Spoon;
import com.yanqu.road.entity.activity.chefduel.process3.ChefDuel3OpenItem;
import com.yanqu.road.entity.enums.activity.chefDuel.eChefDuelActivityType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.activity.chefduel.ChefDuel2Config;
import com.yanqu.road.logic.activity.chefduel.ChefDuel3Config;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.chefduel.ChefDuelActivityProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.chefduel.ChefDuelActivityModule;
import com.yanqu.road.server.gameplayer.module.chefduel.ChefDuelModule;
import com.yanqu.road.server.logic.activity.relation.RelationActivityData;
import com.yanqu.road.server.manger.activity.RelationActivityMgr;
import com.yanqu.road.server.manger.activity.RelationActivityRankMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.ChefDuelActivityPb;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 厨神争霸活动加载mgr
 */
public class ChefDuelActivityMgr extends RelationActivityMgr {

    private static Logger logger = LogManager.getLogger(ChefDuelActivityMgr.class.getName());

    //配置消息map
    private static Map<Integer, ChefDuel2Config> chefDuel2ConfigMap = new ConcurrentHashMap<>();

    private static Map<Integer, ChefDuel3Config> chefDuel3ConfigMap = new ConcurrentHashMap<>();

    public static void reloadActivityData(){
        logger.info("reload ChefDuelActivityMgr start");
        // 获取活动
        List<ActivityInfo> openActivityInfoList = getOpenActivityInfoList(eActivityType.ChefDuel.getValue());
        addChildEvent(openActivityInfoList);
        if (openActivityInfoList.size() <= 0) {
            return;
        }

//        Map<Integer, ChefDuel1Config> tmpChefDuel1ConfigMap = new ConcurrentHashMap<>();
        Map<Integer, ChefDuel2Config> tmpChefDuel2ConfigMap = new ConcurrentHashMap<>();
        Map<Integer, ChefDuel3Config> tmpChefDuel3ConfigMap = new ConcurrentHashMap<>();


        List<Integer> activityIdList = new ArrayList<>();
        for(ActivityInfo activityInfo : openActivityInfoList){
            activityIdList.add(activityInfo.getActivityId());
            activityInfoMap.put(activityInfo.getActivityId(),activityInfo);
        }
        Map<Integer, Map<String, ActivityConfig>> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList);
        for (ActivityInfo activityInfo : openActivityInfoList) {
            if (activityInfo.getChildType() == eChefDuelActivityType.Phase1.getChildType()){
//                ChefDuel1Config config = new ChefDuel1Config(activityInfo,activityConfigMap.get(activityInfo.getActivityId()));
//                tmpChefDuel1ConfigMap.put(activityInfo.getActivityId(),config);
            } else if (activityInfo.getChildType() == eChefDuelActivityType.Phase2.getChildType()){
                ChefDuel2Config config = new ChefDuel2Config(activityInfo,activityConfigMap.get(activityInfo.getActivityId()));
                tmpChefDuel2ConfigMap.put(activityInfo.getActivityId(),config);
            } else if (activityInfo.getChildType() == eChefDuelActivityType.Phase3.getChildType()){
                ChefDuel3Config config = new ChefDuel3Config(activityInfo,activityConfigMap.get(activityInfo.getActivityId()));
                tmpChefDuel3ConfigMap.put(activityInfo.getActivityId(),config);
            }
        }

//        chefDuel1ConfigMap = tmpChefDuel1ConfigMap;
        chefDuel2ConfigMap = tmpChefDuel2ConfigMap;
        chefDuel3ConfigMap = tmpChefDuel3ConfigMap;


        for (ActivityInfo activityInfo : openActivityInfoList) {
            //去加载对应逻辑类的方法
            ChefDuelMgr.loadActivity(activityInfo.getActivityId());
        }
        //hyq 这会连着小当家的一起刷了
//        RelationActivityRankMgr.reloadRank();
        //刷新在线玩家
        playerReloadActivity();
        ChefDuelMgr.reloadActivity();
        ChefDuelMgr.removeActivity();
        logger.info("reload ChefDuelActivityMgr end");
    }

    /**
     * 二、三阶段
     * ========================================
     */
    public static void addChildEvent(List<ActivityInfo> activityInfoList){
        for(ActivityInfo activityInfo : activityInfoList){
            if(activityInfo.getChildType() == eChefDuelActivityType.Phase1.getChildType()){
//                Map<Integer,Map<Integer,Integer>> eventTypeMap = new ConcurrentHashMap<>();
//                Map<Integer,Integer> tempEventMap = new ConcurrentHashMap<>();
//                tempEventMap.put(RelationActivityMgr.EVENT_RANK_USER_TYPE, eGamePlayerEventType.ChefDuel1StageScoreRank.getValue());
//
//                eventTypeMap.put(eGamePlayerEventType.ChefDuel1StageScoreRank.getValue(),tempEventMap);
//                addEventRelationMap(activityInfo.getActivityId(),eventTypeMap);
//
//                Map<Integer,Integer> tempRankMap = new ConcurrentHashMap<>();
//                tempRankMap.put(RelationActivityMgr.EVENT_RANK_USER_TYPE, eGamePlayerEventType.ChefDuel1StageScoreRank.getValue());
//                addRankEventMap(activityInfo.getActivityId(),tempRankMap);
            }
            if(activityInfo.getChildType() == eChefDuelActivityType.Phase2.getChildType()){
                Map<Integer,Map<Integer,Integer>> eventTypeMap = new ConcurrentHashMap<>();
                Map<Integer,Integer> tempEventMap = new ConcurrentHashMap<>();
                tempEventMap.put(RelationActivityMgr.EVENT_RANK_USER_TYPE, eGamePlayerEventType.ChefDuel2IntegralUserCrossRank.getValue());
                eventTypeMap.put(eGamePlayerEventType.ChefDuel2IntegralUserCrossRank.getValue(),tempEventMap);
                addEventRelationMap(activityInfo.getActivityId(),eventTypeMap);

                Map<Integer,Integer> tempRankMap = new ConcurrentHashMap<>();
                tempRankMap.put(RelationActivityMgr.EVENT_RANK_USER_TYPE, eGamePlayerEventType.ChefDuel2IntegralUserCrossRank.getValue());
                addRankEventMap(activityInfo.getActivityId(),tempRankMap);
            }
            if(activityInfo.getChildType() == eChefDuelActivityType.Phase3.getChildType()){
                Map<Integer,Map<Integer,Integer>> eventTypeMap = new ConcurrentHashMap<>();
                Map<Integer,Integer> tempEventMap = new ConcurrentHashMap<>();
                tempEventMap.put(RelationActivityMgr.EVENT_RANK_USER_TYPE, eGamePlayerEventType.ChefDuel3IntegralUserCrossRank.getValue());
                eventTypeMap.put(eGamePlayerEventType.ChefDuel3IntegralUserCrossRank.getValue(),tempEventMap);
                addEventRelationMap(activityInfo.getActivityId(),eventTypeMap);

                Map<Integer,Integer> tempRankMap = new ConcurrentHashMap<>();
                tempRankMap.put(RelationActivityMgr.EVENT_RANK_USER_TYPE, eGamePlayerEventType.ChefDuel3IntegralUserCrossRank.getValue());
                addRankEventMap(activityInfo.getActivityId(),tempRankMap);
            }

        }

    }

    public static Map<Integer, ChefDuel2DishConfig> getActivityDishConfigMap(int activityId){
        ChefDuel2Config chefDuel2Config = chefDuel2ConfigMap.get(activityId);
        if(chefDuel2Config == null){
            return Collections.emptyMap();
        }
        return chefDuel2Config.getDishConfigMap();
    }

    //是不是跑马灯品质
    public static boolean isHorseNoticeQuality(int activityId, int dishId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config == null){
            return false;
        }
        return chefDuel2Config.isHorseNoticeQuality(dishId);
    }

    public static ChefDuel2DishConfig getDishConfig(int activityId, int dishId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config != null){
            return chefDuel2Config.getDishConfig(dishId);
        }
        return null;
    }



    public static String getWorship2Reward(int activityId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config == null){
            return "";
        }
        return chefDuel2Config.getWorship2Reward();
    }

    public static String getWorship3Reward(int activityId){
        ChefDuel3Config chefDuel3Config = getChefDuel3Config(activityId);
        if(chefDuel3Config == null){
            return "";
        }
        return chefDuel3Config.getWorship3Reward();
    }

    public static List<Integer> getChefDuel2SpoonIdList(int activityId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config != null){
            return chefDuel2Config.getChefDuel2SpoonIdList();
        }
        return Collections.emptyList();
    }

    public static ChefDuel2Spoon getChefDuel2Spoon(int activityId, int spoonId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config != null){
            return chefDuel2Config.getChefDuel2Spoon(spoonId);
        }
        return null;
    }

    public static List<String> randomPickReward(int activityId,int times){
        List<String> rewardList = new ArrayList<>();
        for(int i = times;i > 0;i--){
            rewardList.add(randomPickReward(activityId));
        }
        return rewardList;
    }

    public static String randomPickReward(int activityId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config == null){
            return "";
        }
        return chefDuel2Config.randomPickReward();
    }


    public static int getChefDuel2IntegralItemId(int activityId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config == null){
            return 0;
        }
        return chefDuel2Config.getChefDuel2IntegralItemId();
    }

    public static int getChefDuel3IntegralItemId(int activityId){
        ChefDuel3Config chefDuel3Config = getChefDuel3Config(activityId);
        if(chefDuel3Config == null){
            return 0;
        }
        return chefDuel3Config.getChefDuel3IntegralItemId();
    }

    public static int getChefDuel2PickUpItemId(int activityId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config == null){
            return 0;
        }
        return chefDuel2Config.getChefDuel2PickUpItemId();
    }

    public static int getChefDuel2RecoveryTime(int activityId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config == null){
            return 0;
        }
        return chefDuel2Config.getChefDuel2RecoveryTime();
    }

    public static int getChefDuel2MaxHp(int activityId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config == null){
            return 0;
        }
        return chefDuel2Config.getChefDuel2MaxHp();
    }

    //是否可以直接登录就参加
    public static boolean isChefDuel2Able(int activityId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config == null){
            return false;
        }
        return chefDuel2Config.isChefDuel2Able();
    }

    public static int getChefDuel2Hp(int activityId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config == null){
            return 0;
        }
        return chefDuel2Config.getChefDuel2Hp();
    }

    public static ChefDuel2Spoon getChefDuel2MinSpoon(int activityId){
        ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
        if(chefDuel2Config == null){
            return null;
        }
        return chefDuel2Config.getChefDuel2MinSpoon();
    }

    public static ChefDuel3OpenItem getChefDuel3OpenItem(int activityId, int itemId){
        ChefDuel3Config chefDuel3Config = getChefDuel3Config(activityId);
        if(chefDuel3Config == null){
            return null;
        }
        return chefDuel3Config.getChefDuel3OpenItem(itemId);
    }

    public static String getChefDuel3OpenJoinerReward(int activityId, int itemId){
        ChefDuel3Config chefDuel3Config = getChefDuel3Config(activityId);
        if(chefDuel3Config == null){
            return null;
        }
        return chefDuel3Config.getChefDuel3OpenJoinerReward(itemId);
    }

    //挑菜
    public static ChefDuel2DishConfig randomPickDishId(int activityId){
        ChefDuel2DishConfig randomDish = null;
        Map<Integer, ChefDuel2DishConfig> dishMap = getActivityDishConfigMap(activityId);
        int totalWeight = 0;
        for (ChefDuel2DishConfig cookBoy2DishConfig : dishMap.values()) {
            int weight = cookBoy2DishConfig.getWeight();
            totalWeight += weight;
        }
        int randomNum = randomHelper.next(0, totalWeight);
        for (ChefDuel2DishConfig cookBoy2DishConfig : dishMap.values()) {
            int value = cookBoy2DishConfig.getWeight();
            if (randomNum < value) {
                randomDish = cookBoy2DishConfig;
                break;
            }
            randomNum -= value;
        }
        return randomDish;
    }

    public static boolean isActivityMatchTime(ActivityInfo activityInfo){
        if(null == activityInfo){
            return false;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        if(nowTime < activityInfo.getBeginTime() && nowTime > activityInfo.getEndTime()){
            return false;
        }
        int nowHour = DateHelper.getHours();
        if(activityInfo.getChildType() == eChefDuelActivityType.Phase1.getChildType()){
            //早上10点到晚上10点才会进行
//            if(nowHour < getServeFoodStartHour(activityInfo.getActivityId()) || nowHour >= getServeFoodEndHour(activityInfo.getActivityId())){
//                return false;
//            }
        }else if(activityInfo.getChildType() == eChefDuelActivityType.Phase2.getChildType()){
            //早上10点到晚上10点才会进行
            /*if(nowHour < getServeDishStartHour(activityInfo.getActivityId()) || nowHour >= getServeDishEndHour(activityInfo.getActivityId())){
                return false;
            }*/
        }else if(activityInfo.getChildType() == eChefDuelActivityType.Phase3.getChildType()){
            //早上10点到晚上10点才会进行
            /*if(nowHour < getServeBanquetStartHour(activityInfo.getActivityId()) || nowHour >= getServeBanquetEndHour(activityInfo.getActivityId())){
                return false;
            }*/
        }

        return true;
    }


    private static void playerReloadActivity() {
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
        playerList.forEach(action -> {
            action.getModule(ChefDuelActivityModule.class).addNewActivity();
            action.getModule(ChefDuelActivityModule.class).syncActivity();
            action.getModule(ChefDuelModule.class).loadData();
            action.getModule(ChefDuelModule.class).loginSendMsg();
        });
    }

    public static ChefDuelActivityProto.ChefDuelActivityDetailMsg.Builder getChefDuelActivityDetailMsg(Map<Integer,Map<Integer, RelationActivityData>> activityDataMap, String language){
        return ChefDuelActivityPb.parseChefDuelActivityDetailMsg(activityDataMap, getOpenActivityInfoList(eActivityType.ChefDuel.getValue()), activityConditionInfoMap, activityMallInfoMap, language);
    }



    public static Map<Integer, ChefDuel2Config> getChefDuel2ConfigMap() {
        return chefDuel2ConfigMap;
    }

    public static Map<Integer, ChefDuel3Config> getChefDuel3ConfigMap() {
        return chefDuel3ConfigMap;
    }


    public static ChefDuel2Config getChefDuel2Config(int activityId){
        return chefDuel2ConfigMap.get(activityId);
    }

    public static ChefDuel3Config getChefDuel3Config(int activityId){
        return chefDuel3ConfigMap.get(activityId);
    }

    public static Map<String, ActivityConfig> getActivityConfigMap(int childType, int activityId) {
        if(childType == eChefDuelActivityType.Phase2.getChildType()){
            ChefDuel2Config chefDuel2Config = getChefDuel2Config(activityId);
            if(chefDuel2Config != null){
                return chefDuel2Config.getActivityConfigMap();
            }
        }else if(childType == eChefDuelActivityType.Phase3.getChildType()){
            ChefDuel3Config chefDuel3Config = getChefDuel3Config(activityId);
            if(chefDuel3Config != null){
                return chefDuel3Config.getActivityConfigMap();
            }
        }
        return Collections.emptyMap();
    }
}
